package com.docmanage.am.service.impl;

import com.docmanage.am.domain.AmAuthRole;
import com.docmanage.am.domain.AmFolder;
import com.docmanage.am.mapper.AmAuthRoleMapper;
import com.docmanage.am.mapper.AmFolderMapper;
import com.docmanage.common.utils.SecurityUtils;
import com.docmanage.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 按角色授权Service业务层处理
 * 
 * @author pointLion
 * @date 2023-07-10
 */
@Service
public class AmAuthRoleService
{
    @Autowired
    private AmAuthRoleMapper amAuthRoleMapper;
    @Autowired
    private AmFolderMapper amFolderMapper;
    @Autowired
    private AmFolderService amFolderService;

    /**
     * 查询按角色授权
     * 
     * @param id 按角色授权主键
     * @return 按角色授权
     */
    public AmAuthRole selectAmAuthRoleById(Long id)
    {
        return amAuthRoleMapper.selectAmAuthRoleById(id);
    }

    /**
     * 查询按角色授权列表
     * 
     * @param amAuthRole 按角色授权
     * @return 按角色授权
     */
    public List<AmAuthRole> selectAmAuthRoleList(AmAuthRole amAuthRole)
    {
        return amAuthRoleMapper.selectAmAuthRoleList(amAuthRole);
    }

    /**
     * 新增按角色授权
     * 
     * @param amAuthRole 按角色授权
     * @return 结果
     */
    public int insertAmAuthRole(AmAuthRole amAuthRole)
    {
        return amAuthRoleMapper.insertAmAuthRole(amAuthRole);
    }

    /**
     * 修改按角色授权
     * 
     * @param amAuthRole 按角色授权
     * @return 结果
     */
    public int updateAmAuthRole(AmAuthRole amAuthRole)
    {
        return amAuthRoleMapper.updateAmAuthRole(amAuthRole);
    }

    /**
     * 批量删除按角色授权
     * 
     * @param ids 需要删除的按角色授权主键
     * @return 结果
     */
    public int deleteAmAuthRoleByIds(Long[] ids)
    {
        return amAuthRoleMapper.deleteAmAuthRoleByIds(ids);
    }

    /**
     * 删除按角色授权信息
     * 
     * @param id 按角色授权主键
     * @return 结果
     */
    public int deleteAmAuthRoleById(Long id)
    {
        return amAuthRoleMapper.deleteAmAuthRoleById(id);
    }



    public List<AmAuthRole> listAuthFolder(Long roleId){
        return amAuthRoleMapper.listAuthFolder(roleId);
    }


    /*****
     * 给文件夹授权
     */
    public void allotFolder(AmAuthRole role){
        //公共属性
        Date date = new Date();
        String type = "0";
        role.setType(type);
        role.setOperateTime(date);
        role.setOperateBy(SecurityUtils.getUserId());
        //如果之前就有---先操作自己
        if(role.getId()!=null){
            amAuthRoleMapper.updateAmAuthRole(role);
        }else {
            amAuthRoleMapper.insertAmAuthRole(role);
        }
        // 如果范围是影响下级所有
        if("1".equals(role.getRange())){
            // 先删除当前等级下的所有权限
            Long folderId = role.getFolderFileId();
            AmFolder folder = amFolderMapper.selectAmFolderById(folderId);
            String cpath = (StringUtils.isNotBlank(folder.getCpath())?folder.getCpath():"") +"/"+folder.getId();
            amAuthRoleMapper.deleteByAllotFolder(cpath);
            // 再批量插入新的数据
            List<AmFolder> folderList =  amFolderService.selectAllChildren(folder);
            List<AmAuthRole> batchList = new ArrayList<>();
            for(AmFolder f : folderList){
                AmAuthRole i = new AmAuthRole();
                i.setOperateBy(SecurityUtils.getUserId());
                i.setOperateTime(date);
                i.setRoleId(role.getRoleId());
                i.setType(type);
                i.setRange("0");//孩子都是自己
                i.setAuthView(role.getAuthView());
                i.setAuthAdd(role.getAuthAdd());
                i.setAuthUpdate(role.getAuthUpdate());
                i.setAuthViewFileInFolder(role.getAuthViewFileInFolder());
                i.setAuthDelete(role.getAuthDelete());
                i.setFolderFileId(f.getId());
                batchList.add(i);
            }
            if(folderList.size()>0){
                amAuthRoleMapper.batchInsert(batchList);
            }
        }
    }
}
